1 using UnityEngine;
2 using
System.Collections;
3 using
Assets.Scripts;
4
5 [RequireComponent(
typeof(Rigidbody2D))]
6 public
class Bird : MonoBehaviour
7 {
8
9     
// Use this for initialization
10     
void Start()
11     {
12         
//trailrenderer is not visible until we throw the bird
13         GetComponent<TrailRenderer>().enabled =
false;
14         GetComponent<TrailRenderer>().sortingLayerName =
"Foreground";
15         
//no gravity at first
16         GetComponent<Rigidbody2D>().isKinematic =
true;
17         
//make the collider bigger to allow for easy touching
18         GetComponent<CircleCollider2D>().radius = Constants.BirdColliderRadiusBig;
19         State = BirdState.BeforeThrown;
20     }
21
22
23
24     
void FixedUpdate()
25     {
26         
//if we've thrown the bird
27         
//and its speed is very small
28         
if (State == BirdState.Thrown &&
29             GetComponent<Rigidbody2D>().velocity.sqrMagnitude <= Constants.MinVelocity)
30         {
31             
//destroy the bird after 2 seconds
32             StartCoroutine(DestroyAfter(
2));
33         }
34     }
35
36     
public void OnThrow()
37     {
38         
//play the sound
39         GetComponent<AudioSource>().Play();
40         
//show the trail renderer
41         GetComponent<TrailRenderer>().enabled =
true;
42         
//allow for gravity forces
43         GetComponent<Rigidbody2D>().isKinematic =
false;
44         
//make the collider normal size
45         GetComponent<CircleCollider2D>().radius = Constants.BirdColliderRadiusNormal;
46         State = BirdState.Thrown;
47     }
48
49     IEnumerator DestroyAfter(
float seconds)
50     {
51         
yield return new WaitForSeconds(seconds);
52         Destroy(gameObject);
53     }
54
55     
public BirdState State
56     {
57         
get;
58         
private set;
59     }
60 }



Trò chơi Angry Birds trong UNITY Engine 31.693 lượt xem

Gõ tìm kiếm nhanh...